// pages/goods/commoditylist/commoditylist.js
//获取应用实例，请求数据的方法
const {
  request,
  PageContent
} = require("../../../utils/util.js");
var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    num: 1,                   //默认选中抢购的第一项
    products_data: [
    //{ 
    //   id: "orange", //id
    //   src: "/static/imgs/market/orange.jpg", //primaryImageUrl
    //   description: "赣南脐橙/箱/10斤装", 
    //   product_name: "橙子", //name
    //   now_price: "88", //customerPrice
    //   old_price: "108", //price
    //   sold: "100" //originStock
    //   }, { id: "apple", src: "/static/imgs/market/orange.jpg", description: "富平红富士/箱/10斤装富平红富士/箱/10斤装", product_name: "苹果", now_price: "128", old_price: "158", sold: "200" }, { id: "orange", src: "/static/imgs/market/orange.jpg", description: "赣南脐橙/箱/10斤装", product_name: "橙子", now_price: "88", old_price: "108", sold: "100" }, { id: "apple", src: "/static/imgs/market/orange.jpg", description: "富平红富士/箱/10斤装富平红富士/箱/10斤装", product_name: "苹果", now_price: "128", old_price: "158", sold: "200" }
      ],        //页面秒杀商品的信息
    is_hide_loadmore: true    //控制加载更多的显示
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    util.request(api.commoditiesListByTourismActivityId + options.goodId,'GET').then(function (res) {
      if ('200' == res.code) {
        console.log(res);
        var p_d = res.responseBody;
        var d_p_d = [];
        for(var i=0; i<p_d.length; i++){
          var imgurl = p_d[i].primaryImageUrl
          imgurl = imgurl.substring(2, imgurl.length - 2);
          d_p_d.push({
            id: p_d[i].id,
            src: imgurl,
            name: p_d[i].name,
            now_price: p_d[i].customerPrice/100,
            old_price: p_d[i].price/100,
            sold: p_d[i].originStock
          });
        }
        that.setData({
          products_data: d_p_d
        })
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
     * 点击商品进入商品对应的详情页
     * @pram e--当前点击对象的属性集合
     */
  goDetail: (e) => {
    // 带上商品id跳转商品详情页
    PageContent.loadNavigate(`../../market/cart/goods_detail/goods_detail?product_id=${e.currentTarget.dataset.id}`);
  }
})